package offer;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Elephas
 * @Date 2022/1/30
 **/
@FunctionalInterface
public interface UncommonFromSentences {
    String[] uncommonFromSentences(String s1, String s2);
}
class UncommonFromSentencesImpl1 implements UncommonFromSentences{
    static final String TEST_01_S1 = "apple apple";
    static final String TEST_02_S2 = "banana";

    public static void main(String[] args) {
        new UncommonFromSentencesImpl1().uncommonFromSentences(TEST_01_S1,TEST_02_S2);
    }
    @Override
    public String[] uncommonFromSentences(String s1, String s2) {

        Set<String> set1 = new HashSet<>();
        String[] str1 = s1.split(" ");
        Arrays.sort(str1);
        int str1Len = 0;
        for (int i = 1; i < str1.length; i++) {
            if(!str1[i].equals(str1[i-1])){
                str1Len++;
                str1[str1Len] = str1[i];
            }
        }
        for (int i = 0; i <= str1Len; i++) {
            set1.add(str1[i]);
        }

        Set<String> set2 = new HashSet<>();
        String[] str2 = s2.split(" ");
        Arrays.sort(str2);
        int str2Len = 0;
        for (int i = 1; i < str2.length; i++) {
            if(str2[i] == str2[i-1]){
                str2Len++;
                str2[str2Len] = str2[i];
            }
        }
        for (int i = 0; i < str2Len; i++) {
            set2.add(str2[i]);
        }
        //Set<String> set2 = Arrays.stream(s2.split(" ")).collect(Collectors.toSet());
        Set<String> set1AndSet2 = set1.stream().filter(element->{return set2.contains(element);}).collect(Collectors.toSet());
        Set<String> set1OrSet2 = new HashSet<>();
        set1OrSet2.addAll(set1);
        set1OrSet2.addAll(set2);
        Set<String>  ans = set1OrSet2.stream().filter(element->{return !set1AndSet2.contains(element);}).collect(Collectors.toSet());
        return ans.toArray(new String[0]);
    }
}

class UncommonFromSentencesImpl2 implements UncommonFromSentences{

    @Override
    public String[] uncommonFromSentences(String s1, String s2) {
        String[] strs1 = s1.split(" ");
        String[] strs2 = s2.split(" ");
        if(strs2.length == 0 && strs1.length == 0){
            return new String[0];
        }
        String[] union = new String[strs1.length + strs2.length];
        for (int i = 0; i < strs1.length; i++) {
            union[i] = strs1[i];
        }
        int current = strs1.length;
        for (int i = 0; i < strs2.length; i++) {
            union[i+current] = strs2[i];
        }
        Arrays.sort(union);
        current = 0;
        // str[0] , i == 0, str[0] != str[1]
        // str[i] , 0 < i < str.length - 1, str[i-1] != str[i] && str[i] != str[i+1]
        // str[str.length - 1] , str[i] != str[i-1]
        if(union.length > 1){
            if(union[0] != union[1]){
                current ++;
            }
        }
        for (int i = 1; i < union.length - 1; i++) {
            if(!union[i-1].equals(union[i]) && !union[i+1].equals(union[i])){
                union[current++] = union[i];
            }
        }
        if(union.length > 1){
            if(union[union.length - 1].equals(union[union.length - 2])){
                union[current++] = union[union.length - 1];
            }
        }
        String[] ans = new String[current];
        for (int i = 0; i < current; i++) {
            ans[i] = union[i];
        }
        return ans;
    }
}
class UncommonFromSentencesImpl3 implements UncommonFromSentences{

    @Override
    public String[] uncommonFromSentences(String s1, String s2) {
        String[] strs1 = s1.split(" ");
        String[] strs2 = s2.split(" ");
        Map<String,Integer> map = new HashMap<>();
        for (int i = 0; i < strs1.length; i++) {
            String current = strs1[i];
            if(map.containsKey(current)){
                map.put(current,map.get(current) + 1);
            }else{
                map.put(current,1);
            }
        }
        for (int i = 0; i < strs2.length; i++) {
            String current = strs2[i];
            if(map.containsKey(current)){
                map.put(current,map.get(current) + 1);
            }else{
                map.put(current,1);
            }
        }
        List<String> ans = new ArrayList<>();
        for(Map.Entry<String,Integer> entry : map.entrySet()){
            String key = entry.getKey();
            int value = entry.getValue();
            if(value == 1){
                ans.add(key);
            }
        }
        return ans.toArray(new String[0]);
        /*
        String[] temp = new String[ans.size()];
        for (int i = 0; i < ans.size(); i++) {
            temp[i] = ans.get(i);
        }
        return temp;

         */
    }
}